//
//  MineTableViewController.m
//  LOL视频播放
//
//  Created by 花花蔡 on 2016/12/10.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

#import "MineTableViewController.h"
#import "AboutUsViewController.h"
#import "CLLLastSeeViewController.h"
@interface MineTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSArray *dataArray;

@end

@implementation MineTableViewController
- (void)viewWillAppear:(BOOL)animated {
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 2) {
        return 2;
    }else
        return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        static NSString *cellID = @"ID_1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = self.dataArray[indexPath.row];
        
        return cell;
    }
    if (indexPath.section == 1) {
        static NSString *cellID = @"ID_11";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = self.dataArray[indexPath.row + 1];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.f M",[self filePath]];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:19];
        return cell;
    }
    if (indexPath.section == 2) {
        static NSString *cellID = @"ID__3";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.textLabel.text = self.dataArray[indexPath.row + 2];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.section == 3) {
        static NSString *cellID = @"ID__4";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        }
        cell.textLabel.text = self.dataArray[indexPath.row + 4];
        cell.detailTextLabel.text = @"1.0";
        cell.detailTextLabel.font = [UIFont systemFontOfSize:19];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        //打开观看历史界面
        CLLLastSeeViewController * CLLVC = [[CLLLastSeeViewController alloc] init];
        [self.navigationController pushViewController:CLLVC animated:YES];
    }else if(indexPath.section == 1) {
        //清空缓存
        if ([self filePath] != 0.0) {
            [self clearFile];
        };
        
    }else if (indexPath.section == 2 && indexPath.row == 1) {
        //关于我们
        AboutUsViewController *AVC = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:AVC animated:YES];
    }else if (indexPath.section == 2 && indexPath.row == 2){
        //前往评分
        NSString *URLStr = @"https://www.baidu.com";
        //        if ([UIDevice currentDevice].systemVersion.floatValue > 8.0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLStr] options:@{} completionHandler:nil];
    }
  
}

#pragma mark - 清理缓存
/*
 * 1.显示缓存大小
 */
- (CGFloat)filePath {
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    return [self folderSizeAtPath:cachPath];
}
//遍历文件获得文件大小，单位M
- (CGFloat)folderSizeAtPath:(NSString *)folderPath {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:folderPath]) {
        return 0;
    }
    
    NSEnumerator *enumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString *fileName;
    NSInteger folderSize = 0;
    
    while ((fileName = [enumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize / (1024.0 * 1024);
}
//计算单个文件大小
- (NSInteger)fileSizeAtPath:(NSString *)filePath {
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath]) {
        return  [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

- (void)clearFile {
    //找到沙盒中存放缓存数据的文件夹路径
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    
    [files enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //
        NSError *error = nil;
        
        NSString *path = [cachPath stringByAppendingPathComponent:obj];
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
        }
    }];
    
    [self performSelectorOnMainThread:@selector(clearCachSuccess) withObject:nil waitUntilDone:YES];
}

//清理成功
- (void)clearCachSuccess {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"清理成功" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //刷新某一行cell
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - 懒加载

- (NSArray *)dataArray {
    
    if (!_dataArray) {
        _dataArray = @[@"观看历史",@"清理缓存",@"前往评分",@"关于我们",@"版本信息"];
    }
    return _dataArray;
}

@end
